package com.apartment.controller;


import com.apartment.auto.JSONAuthentication;
import com.apartment.entity.ApHouse;
import com.apartment.entity.ApUser;
import com.apartment.entity.HouseType;
import com.apartment.mapper.ApHouseMapper;
import com.apartment.mapper.ApUserMapper;
import com.apartment.mapper.HouseTypeMapper;
import com.apartment.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author apartment
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/ap-house")
public class ApHouseController extends JSONAuthentication {
    @Autowired
    ApHouseMapper apHouseMapper;
    @Autowired
    HouseTypeMapper houseTypeMapper;
    @Autowired
    ApUserMapper apUserMapper;
    @ApiOperation("添加房间")
    @PostMapping("/add")
    public void addHouse(HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String  name, @RequestParam("type") int type) throws Exception{
       ApHouse apHouse=new ApHouse();
       apHouse.setType(type);
       apHouse.setName(name);
        if(apHouseMapper.insert(apHouse)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除房间")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId ) throws Exception{
        int i=apHouseMapper.deleteById(userId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改房间状态")
    @PostMapping("/updHouseStatus")
    public void updHouseStatus(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("status") String status) throws  Exception{
        ApHouse apHouse=apHouseMapper.selectById(id);
        apHouse.setStatus(status);
        if(apHouseMapper.updateById(apHouse)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询全部空房间")
    @PostMapping("/findAllNullHouse")
    public void findAllNullHouse(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","空闲");
        Page<ApHouse> shUserPage=new Page<>(current,size);
        Page<ApHouse> shUserPages=apHouseMapper.selectPage(shUserPage,wrapper);
        List<ApHouse> records = shUserPages.getRecords();
        int total=apHouseMapper.selectCount(wrapper);
        for (ApHouse a:records){
            a.setHouseType(houseTypeMapper.selectById(a.getType()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("House",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("无空房");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询全部房间")
    @PostMapping("/findAllHouse")
    public void findAllHouse(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{

        Page<ApHouse> shUserPage=new Page<>(current,size);
        Page<ApHouse> shUserPages=apHouseMapper.selectPage(shUserPage,null);
        List<ApHouse> records = shUserPages.getRecords();
        int total=apHouseMapper.selectCount(null);
        for (ApHouse a:records){
            a.setHouseType(houseTypeMapper.selectById(a.getType()));
            if(a.getUserId()>0){
                a.setApUser(apUserMapper.selectById(a.getUserId()));
            }
        }
        if(records.size()>0){
            Result ok = Result.ok().data("House",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("无房间");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "跟具房间类型查找房间")
    @PostMapping("/findAllHouseByType")
    public void findAllHouseByType(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("type")String type)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("name",type);
        HouseType houseType = houseTypeMapper.selectOne(wrapper);
        QueryWrapper wrapper1=new QueryWrapper();
        wrapper1.eq("type",houseType.getId());
        Page<ApHouse> shUserPage=new Page<>(current,size);
        Page<ApHouse> shUserPages=apHouseMapper.selectPage(shUserPage,wrapper1);
        List<ApHouse> records = shUserPages.getRecords();
        int total=apHouseMapper.selectCount(wrapper1);
        for (ApHouse a:records){
            a.setHouseType(houseTypeMapper.selectById(a.getType()));
            if(a.getUserId()>0){
                a.setApUser(apUserMapper.selectById(a.getUserId()));
            }
        }
        if(records.size()>0){
            Result ok = Result.ok().data("House",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("无房间");
            this.WriteJSON(request, response, error);
        }
    }

}

